<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/examples/svgalib.qdoc -->
<head>
  <title>Qt 4.3: Accelerated Graphics Driver Example</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Accelerated Graphics Driver Example<br /><small></small></h1>
<p>Files:</p>
<ul>
<li><a href="qtopiacore-svgalib-svgalibpaintdevice-cpp.html">qtopiacore/svgalib/svgalibpaintdevice.cpp</a></li>
<li><a href="qtopiacore-svgalib-svgalibpaintdevice-h.html">qtopiacore/svgalib/svgalibpaintdevice.h</a></li>
<li><a href="qtopiacore-svgalib-svgalibpaintengine-cpp.html">qtopiacore/svgalib/svgalibpaintengine.cpp</a></li>
<li><a href="qtopiacore-svgalib-svgalibpaintengine-h.html">qtopiacore/svgalib/svgalibpaintengine.h</a></li>
<li><a href="qtopiacore-svgalib-svgalibplugin-cpp.html">qtopiacore/svgalib/svgalibplugin.cpp</a></li>
<li><a href="qtopiacore-svgalib-svgalibscreen-cpp.html">qtopiacore/svgalib/svgalibscreen.cpp</a></li>
<li><a href="qtopiacore-svgalib-svgalibscreen-h.html">qtopiacore/svgalib/svgalibscreen.h</a></li>
<li><a href="qtopiacore-svgalib-svgalibsurface-cpp.html">qtopiacore/svgalib/svgalibsurface.cpp</a></li>
<li><a href="qtopiacore-svgalib-svgalibsurface-h.html">qtopiacore/svgalib/svgalibsurface.h</a></li>
</ul>
<p>The Accelerated Graphics Driver example shows how you can write your own accelerated graphics driver and <a href="qtopiacore-accel.html#add-your-graphics-driver-to-qtopia-core">add your graphics driver to Qtopia Core</a>. In <a href="qtopiacore.html">Qtopia Core</a>, painting is a pure software implementation and is normally performed in two steps: The clients render each window onto a corresponding surface (stored in memory) using a paint engine, and then the server uses the graphics driver to compose the surface images and copy them to the screen. (See the <a href="qtopiacore-architecture.html">Qtopia Core Architecture</a> documentation for details.)</p>
<p>The rendering can be accelerated in two ways: Either by accelerating the copying of pixels to the screen, or by accelerating the explicit painting operations. The first is done in the graphics driver implementation, the latter is performed by the paint engine implementation. Typically, both the pixel copying and the painting operations are accelerated using the following approach:</p>
<ol type="1">
<li><a href="#step-1-creating-a-custom-graphics-driver">Creating a Custom Graphics Driver</a></li>
<li><a href="#step-2-implementing-a-custom-raster-paint-engine">Implementing a Custom Paint Engine</a></li>
<li><a href="#step-3-making-the-widgets-aware-of-the-custom-paint-engine">Making the Widgets Aware of the Custom Paint Engine</a></li>
</ol>
<p>After compiling the example code, install the graphics driver plugin with the command <tt>make install</tt>. To start an application using the graphics driver, you can either set the environment variable <a href="qtopiacore-envvars.html#qws-display">QWS_DISPLAY</a> and then run the application, or you can just run the application using the <tt>-display</tt> switch:</p>
<pre> myApplication -qws -display svgalib</pre>
<p>Note that in order to keep the example as simple as possible, our driver only works with screen modes having 32 bits per pixel and does not work with multiple processes.</p>
<p><table align="center" cellpadding="2" cellspacing="1" border="0">
<thead><tr valign="top" class="qt-style"><th>SVGAlib</th></tr></thead>
<tr valign="top" class="odd"><td>Instead of interfacing the graphics hardware directly, this example relies on <a href="http://www.svgalib.org">SVGAlib</a> being installed on your system. <a href="http://www.svgalib.org">SVGAlib</a> is a small graphics library which provides acceleration for many common graphics cards used on desktop computers. It should work on most workstations and has a small and simple API.</td></tr>
</table></p>
<a name="step-1-creating-a-custom-graphics-driver"></a>
<h2>Step 1: Creating a Custom Graphics Driver</h2>
<p>The custom graphics driver is created by deriving from the <a href="qscreen.html">QScreen</a> class. <a href="qscreen.html">QScreen</a> is the base class for implementing screen/graphics drivers in Qtopia Core.</p>
<pre> class SvgalibScreen : public QScreen
 {
 public:
     SvgalibScreen(int displayId) : QScreen(displayId) {}
     ~SvgalibScreen() {}

     bool connect(const QString &amp;displaySpec);
     bool initDevice();
     void shutdownDevice();
     void disconnect();

     void setMode(int, int, int) {}
     void blank(bool) {}

     void exposeRegion(QRegion r, int changing);
     void blit(const QImage &amp;img, const QPoint &amp;topLeft, const QRegion &amp;region);
     void solidFill(const QColor &amp;color, const QRegion &amp;region);

 private:
     GraphicsContext *context;
 };</pre>
<p>The <a href="qscreen.html#connect">connect()</a>, <a href="qscreen.html#disconnect">disconnect()</a>, <a href="qscreen.html#initDevice">initDevice()</a> and <a href="qscreen.html#shutdownDevice">shutdownDevice()</a> functions are declared as pure virtual functions in <a href="qscreen.html">QScreen</a> and must be implemented. They are used to configure the hardware, or query its configuration: <a href="qscreen.html#connect">connect()</a> and <a href="qscreen.html#disconnect">disconnect()</a> are called by both the server and client processes, while the <a href="qscreen.html#initDevice">initDevice()</a> and <a href="qscreen.html#shutdownDevice">shutdownDevice()</a> functions are only called by the server process.</p>
<p><a href="qscreen.html">QScreen</a>'s <a href="qscreen.html#setMode">setMode()</a> and <a href="qscreen.html#blank">blank()</a> functions are also pure virtual, but our driver's implementations are trivial. The last three functions (<a href="qscreen.html#exposeRegion">exposeRegion()</a>, <a href="qscreen.html#blit">blit()</a> and <a href="qscreen.html#solidFill">solidFill()</a>) are the ones involved in putting pixels on the screen, i.e&#x2e;, we reimplement these functions to perform the pixel copying acceleration.</p>
<p>Finally, the <tt>context</tt> variable is a pointer to a <a href="http://www.svgalib.org">SVGAlib</a> specific type. Note that the details of using the <a href="http://www.svgalib.org">SVGAlib</a> library is beyond the scope of this example.</p>
<a name="svgalibscreen-class-implementation"></a>
<h3>SvgalibScreen Class Implementation</h3>
<p>The <a href="qscreen.html#connect">connect()</a> function is the first function that is called after the constructor returns. It queries <a href="http://www.svgalib.org">SVGAlib</a> about the graphics mode and initializes the variables.</p>
<pre> bool SvgalibScreen::connect(const QString &amp;displaySpec)
 {
     Q_UNUSED(displaySpec);

     int mode = vga_getdefaultmode();
     if (mode &lt;= 0) {
         qCritical(&quot;SvgalibScreen::connect(): invalid vga mode&quot;);
         return false;
     }

     vga_modeinfo *modeinfo = vga_getmodeinfo(mode);

     QScreen::lstep = modeinfo-&gt;linewidth;
     QScreen::dw = QScreen::w = modeinfo-&gt;width;
     QScreen::dh = QScreen::h = modeinfo-&gt;height;
     QScreen::d = modeinfo-&gt;bytesperpixel * 8;
     QScreen::size = QScreen::lstep * dh;
     QScreen::data = 0;

     switch (depth()) {
     case 32:
         setPixelFormat(QImage::Format_ARGB32_Premultiplied);
         break;
     case 16:
         setPixelFormat(QImage::Format_RGB16);
         break;
     default:
         break;
     }

     const int dpi = 72;
     QScreen::physWidth = qRound(QScreen::dw * 25.4 / dpi);
     QScreen::physHeight = qRound(QScreen::dh * 25.4 / dpi);

     return true;
 }</pre>
<p>It is important that the <a href="qscreen.html#connect">connect()</a> function initializes the <tt>data</tt>, <tt>lstep</tt>, <tt>w</tt>, <tt>h</tt>, <tt>dw</tt>, <tt>dh</tt>, <tt>d</tt>, <tt>physWidth</tt> and <tt>physHeight</tt> variables (inherited from <a href="qscreen.html">QScreen</a>) to ensure that the driver is in a state consistent with the driver configuration.</p>
<p>In this particular example we do not have any information of the real physical size of the screen, so we set these values with the assumption of a screen with 72 DPI.</p>
<pre> bool SvgalibScreen::initDevice()
 {
     if (vga_init() != 0) {
         qCritical(&quot;SvgalibScreen::initDevice(): unable to initialize svgalib&quot;);
         return false;
     }

     int mode = vga_getdefaultmode();
     if (vga_setmode(mode) == -1) {
         qCritical(&quot;SvgalibScreen::initialize(): unable to set graphics mode&quot;);
         return false;
     }

     if (gl_setcontextvga(mode) != 0) {
         qCritical(&quot;SvgalibScreen::initDevice(): unable to set vga context&quot;);
         return false;
     }
     context = gl_allocatecontext();
     gl_getcontext(context);

     vga_modeinfo *modeinfo = vga_getmodeinfo(mode);
     if (modeinfo-&gt;flags &amp; IS_LINEAR)
         QScreen::data = vga_getgraphmem();

     QScreenCursor::initSoftwareCursor();
     return true;
 }</pre>
<p>When the <a href="qscreen.html#connect">connect()</a> function returns, the server process calls the <a href="qscreen.html#initDevice">initDevice()</a> function which is expected to do the necessary hardware initialization, leaving the hardware in a state consistent with the driver configuration.</p>
<p>Note that we have chosen to use the software cursor. If you want to use a hardware cursor, you should create a subclass of <a href="qscreencursor.html">QScreenCursor</a>, create an instance of it, and make the global variable <tt>qt_screencursor</tt> point to this instance.</p>
<pre> void SvgalibScreen::shutdownDevice()
 {
     gl_freecontext(context);
     vga_setmode(TEXT);
 }

 void SvgalibScreen::disconnect()
 {
 }</pre>
<p>Before exiting, the server process will call the <a href="qscreen.html#shutdownDevice">shutdownDevice()</a> function to do the necessary hardware cleanup. Again, it is important that the function leaves the hardware in a state consistent with the driver configuration. When <a href="qscreen.html#shutdownDevice">shutdownDevice()</a> returns, the <a href="qscreen.html#disconnect">disconnect()</a> function is called. Our implementation of the latter function is trivial.</p>
<p>Note that, provided that the <tt>QScreen::data</tt> variable points to a valid linear framebuffer, the graphics driver is fully functional as a simple screen driver at this point. The rest of this example will show where to take advantage of the accelerated capabilities available on the hardware.</p>
<pre> void SvgalibScreen::exposeRegion(QRegion region, int changing)
 {
     QScreen::exposeRegion(region, changing);
     <span class="comment">//</span> flip between buffers if implementing a double buffered driver
 }</pre>
<p>The next function we implement is the <a href="qscreen.html#exposeRegion">exposeRegion()</a> function that paints the given region on screen.</p>
<p>The default implementation will do the necessary composing of the top-level windows and call <a href="qscreen.html#solidFill">solidFill()</a> and <a href="qscreen.html#blit">blit()</a> whenever it is required. We do not want to change this behavior in this driver so we just call the base class implementation. On the other hand, if you are implementing a double buffered screen driver, this is where you would like to flip between the buffers.</p>
<pre> void SvgalibScreen::solidFill(const QColor &amp;color, const QRegion &amp;reg)
 {
     if (depth() != 32 &amp;&amp; depth() != 16) {
         if (base())
             QScreen::solidFill(color, reg);
         return;
     }

     const QVector&lt;QRect&gt; rects = (reg &amp; region()).rects();
     for (int i = 0; i &lt; rects.size(); ++i) {
         const QRect r = rects.at(i);
         gl_fillbox(r.left(), r.top(), r.width(), r.height(), color.rgba());
     }
 }

 void SvgalibScreen::blit(const QImage &amp;img, const QPoint &amp;topLeft,
                          const QRegion &amp;reg)
 {
     if (img.format() != pixelFormat()) {
         if (base())
             QScreen::blit(img, topLeft, reg);
         return;
     }

     const QVector&lt;QRect&gt; rects = (reg &amp; region()).rects();

     for (int i = 0; i &lt; rects.size(); ++i) {
         const QRect r = rects.at(i);
         gl_putboxpart(r.x(), r.y(), r.width(), r.height(),
                       img.width(), img.height(),
                       static_cast&lt;void*&gt;(const_cast&lt;uchar*&gt;(img.bits())),
                       r.x() - topLeft.x(), r.y() - topLeft.y());
     }
 }</pre>
<p>Finally, we accelerate the copying of pixels to the screen by reimplementing the <a href="qscreen.html#solidFill">solidFill()</a> and <a href="qscreen.html#blit">blit()</a> functions.</p>
<a name="step-2-implementing-a-custom-raster-paint-engine"></a>
<h2>Step 2: Implementing a Custom Raster Paint Engine</h2>
<p><a href="qtopiacore.html">Qtopia Core</a> uses <a href="qrasterpaintengine.html">QRasterPaintEngine</a> (a raster-based implementation of <a href="qpaintengine.html">QPaintEngine</a>) to implement the painting operations.</p>
<p>Acceleration of the painting operations is done by deriving from <a href="qrasterpaintengine.html">QRasterPaintEngine</a> class. This is a powerful mechanism for accelerating graphic primitives while getting software fallbacks for all the primitives you do not accelerate.</p>
<pre> #include &lt;private/qpaintengine_raster_p.h&gt;

 class SvgalibPaintEngine : public QRasterPaintEngine
 {
 public:
     SvgalibPaintEngine();
     ~SvgalibPaintEngine();

     bool begin(QPaintDevice *device);
     bool end();
     void updateState(const QPaintEngineState &amp;state);
     void drawRects(const QRect *rects, int rectCount);

 private:
     void setClip(const QRegion &amp;region);
     void updateClip();

     QPen pen;
     bool simplePen;
     QBrush brush;
     bool simpleBrush;
     QMatrix matrix;
     bool simpleMatrix;
     QRegion clip;
     bool clipEnabled;
     bool simpleClip;
     bool opaque;
     bool aliased;
     bool sourceOver;
     QPaintDevice *device;
 };</pre>
<p>In this example, we will only accelerate one of the <a href="qpaintengine.html#drawRects-2">drawRects()</a> functions, i.e&#x2e;, only non-rotated, aliased and opaque rectangles will be rendered using accelerated painting. All other primitives are rendered using the base class's unaccelerated implementation.</p>
<p>The paint engine's state is stored in the private member variables, and we reimplement the <a href="qpaintengine.html#updateState">updateState()</a> function to ensure that our custom paint engine's state is updated properly whenever it is required. The private <tt>setClip()</tt> and <tt>updateClip()</tt> functions are only helper function used to simplify the <a href="qpaintengine.html#updateState">updateState()</a> implementation.</p>
<p>We also reimplement <a href="qrasterpaintengine.html">QRasterPaintEngine</a>'s <a href="qpaintengine.html#begin">begin()</a> and <a href="qpaintengine.html#end">end()</a> functions to initialize the paint engine and to do the cleanup when we are done rendering, respectively.</p>
<p><table align="center" cellpadding="2" cellspacing="1" border="0">
<thead><tr valign="top" class="qt-style"><th>Private Header Files</th></tr></thead>
<tr valign="top" class="odd"><td>Note the <tt>include</tt> statement used by this class. The files prefixed with <tt>private/</tt> are private headers file within <a href="qtopiacore.html">Qtopia Core</a>. Private header files are not part of a <a href="qtopiacore.html">Qtopia Core</a> installation and are only present while compiling <a href="qtopiacore.html">Qtopia Core</a>. To be able to compile using private header files you need to use a <tt>qmake</tt> binary within a compiled <a href="qtopiacore.html">Qtopia Core</a> package.<p><b>Warning:</b> Private header files may change without notice between releases.</p>
</td></tr>
</table></p>
<p>The <a href="qpaintengine.html#begin">begin()</a> function initializes the internal state of the paint engine. Note that it also calls the base class implementation to initialize the parts inherited from <a href="qrasterpaintengine.html">QRasterPaintEngine</a>:</p>
<pre> bool SvgalibPaintEngine::begin(QPaintDevice *dev)
 {
     device = dev;
     pen = Qt::NoPen;
     simplePen = true;
     brush = Qt::NoBrush;
     simpleBrush = true;
     matrix = QMatrix();
     simpleMatrix = true;
     setClip(QRect(0, 0, device-&gt;width(), device-&gt;height()));
     opaque = true;
     aliased = true;
     sourceOver = true;

     return QRasterPaintEngine::begin(dev);
 }

 bool SvgalibPaintEngine::end()
 {
     gl_setclippingwindow(0, 0, device-&gt;width() - 1, device-&gt;height() - 1);
     return QRasterPaintEngine::end();
 }</pre>
<p>The implementation of the <a href="qpaintengine.html#end">end()</a> function removes the clipping constraints that might have been set in <a href="http://www.svgalib.org">SVGAlib</a>, before calling the corresponding base class implementation.</p>
<pre> void SvgalibPaintEngine::updateState(const QPaintEngineState &amp;state)
 {
     QPaintEngine::DirtyFlags flags = state.state();

     if (flags &amp; DirtyTransform) {
         matrix = state.matrix();
         simpleMatrix = (matrix.m12() == 0 &amp;&amp; matrix.m21() == 0);
     }

     if (flags &amp; DirtyPen) {
         pen = state.pen();
         simplePen = (pen.width() == 0 || pen.widthF() &lt;= 1)
                     &amp;&amp; (pen.style() == Qt::NoPen || pen.style() == Qt::SolidLine)
                     &amp;&amp; (pen.color().alpha() == 255);
     }

     if (flags &amp; DirtyBrush) {
         brush = state.brush();
         simpleBrush = (brush.style() == Qt::SolidPattern
                        || brush.style() == Qt::NoBrush)
                       &amp;&amp; (brush.color().alpha() == 255);
     }

     if (flags &amp; DirtyClipRegion)
         setClip(state.clipRegion());

     if (flags &amp; DirtyClipEnabled) {
         clipEnabled = state.isClipEnabled();
         updateClip();
     }

     if (flags &amp; DirtyClipPath) {
         setClip(QRegion());
         simpleClip = false;
     }

     if (flags &amp; DirtyCompositionMode) {
         const QPainter::CompositionMode m = state.compositionMode();
         sourceOver = (m == QPainter::CompositionMode_SourceOver);
     }

     if (flags &amp; DirtyOpacity)
         opaque = (state.opacity() == 256);

     if (flags &amp; DirtyHints)
         aliased = !(state.renderHints() &amp; QPainter::Antialiasing);

     QRasterPaintEngine::updateState(state);
 }</pre>
<p>The <a href="qpaintengine.html#updateState">updateState()</a> function updates our custom paint engine's state. The <a href="qpaintenginestate.html">QPaintEngineState</a> class provides information about the active paint engine's current state.</p>
<p>Note that we only accept and save the current matrix if it doesn't do any shearing. The pen is accepted if it is opaque and only one pixel wide. The rest of the engine's properties are updated following the same pattern. Again it is important that the <a href="qpaintengine.html#updateState">QRasterPaintEngine::updateState</a>() function is called to update the parts inherited from the base class.</p>
<pre> void SvgalibPaintEngine::setClip(const QRegion &amp;region)
 {
     if (region.isEmpty())
         clip = QRect(0, 0, device-&gt;width(), device-&gt;height());
     else
         clip = matrix.map(region) &amp; QRect(0, 0, device-&gt;width(), device-&gt;height());
     clipEnabled = true;
     updateClip();
 }

 void SvgalibPaintEngine::updateClip()
 {
     QRegion clipRegion = QRect(0, 0, device-&gt;width(), device-&gt;height());

     if (!systemClip().isEmpty())
         clipRegion &amp;= systemClip();
     if (clipEnabled)
         clipRegion &amp;= clip;

     simpleClip = (clipRegion.rects().size() &lt;= 1);

     const QRect r = clipRegion.boundingRect();
     gl_setclippingwindow(r.left(), r.top(),
                          r.x() + r.width(),
                          r.y() + r.height());
 }</pre>
<p>The <tt>setClip()</tt> helper function is called from our custom implementation of <a href="qpaintengine.html#updateState">updateState()</a>, and enables clipping to the given region. An empty region means that clipping is disabled.</p>
<p>Our custom update function also makes use of the <tt>updateClip()</tt> helper function that checks if the clip is &quot;simple&quot;, i.e&#x2e;, that it can be represented by only one rectangle, and updates the clip region in <a href="http://www.svgalib.org">SVGAlib</a>.</p>
<pre> void SvgalibPaintEngine::drawRects(const QRect *rects, int rectCount)
 {
     const bool canAccelerate = simplePen &amp;&amp; simpleBrush &amp;&amp; simpleMatrix
                                &amp;&amp; simpleClip &amp;&amp; opaque &amp;&amp; aliased
                                &amp;&amp; sourceOver;
     if (!canAccelerate) {
         QRasterPaintEngine::drawRects(rects, rectCount);
         return;
     }

     for (int i = 0; i &lt; rectCount; ++i) {
         const QRect r = matrix.mapRect(rects[i]);
         if (brush != Qt::NoBrush) {
             gl_fillbox(r.left(), r.top(), r.width(), r.height(),
                        brush.color().rgba());
         }
         if (pen != Qt::NoPen) {
             const int c = pen.color().rgba();
             gl_hline(r.left(), r.top(), r.right(), c);
             gl_hline(r.left(), r.bottom(), r.right(), c);
             gl_line(r.left(), r.top(), r.left(), r.bottom(), c);
             gl_line(r.right(), r.top(), r.right(), r.bottom(), c);
         }
     }
 }</pre>
<p>Finally, we accelerated that drawing of non-rotated, aliased and opaque rectangles in our reimplementation of the <a href="qpaintengine.html#drawRects-2">drawRects()</a> function. The <a href="qrasterpaintengine.html">QRasterPaintEngine</a> fallback is used whenever the rectangle is not simple enough.</p>
<a name="step-3-making-the-widgets-aware-of-the-custom-paint-engine"></a>
<h2>Step 3: Making the Widgets Aware of the Custom Paint Engine</h2>
<p>To activate the custom paint engine, we also need to implement a corresponding paint device and window surface and make some minor adjustments of the graphics driver.</p>
<ul>
<li><a href="#implementing-a-custom-paint-device">Implementing a Custom Paint Device</a></li>
<li><a href="#implementing-a-custom-window-surface">Implementing a Custom Window Surface</a></li>
<li><a href="#adjusting-the-graphics-driver">Adjusting the Graphics Driver</a></li>
</ul>
<a name="implementing-a-custom-paint-device"></a>
<h3>Implementing a Custom Paint Device</h3>
<p>The custom paint device can be derived from the <a href="qcustomrasterpaintdevice.html">QCustomRasterPaintDevice</a> class. Reimplement its <a href="qpaintdevice.html#paintEngine">paintEngine()</a> and <a href="qcustomrasterpaintdevice.html#memory">memory()</a> functions to activate the accelerated paint engine:</p>
<pre> class SvgalibPaintDevice : public QCustomRasterPaintDevice
 {
 public:
     SvgalibPaintDevice(QWidget *w);
     ~SvgalibPaintDevice();

     void* memory() const { return QScreen::instance()-&gt;base(); }

     QPaintEngine *paintEngine() const { return pengine; }
     int metric(PaintDeviceMetric m) const;

 private:
     SvgalibPaintEngine *pengine;
 };</pre>
<p>The <a href="qpaintdevice.html#paintEngine">paintEngine()</a> function should return an instance of the <tt>SvgalibPaintEngine</tt> class. The <a href="qcustomrasterpaintdevice.html#memory">memory()</a> function should return a pointer to the buffer which should be used when drawing the widget.</p>
<p>Our example driver is rendering directly to the screen without any buffering, i.e&#x2e;, our custom pain device's <a href="qcustomrasterpaintdevice.html#memory">memory()</a> function returns a pointer to the framebuffer. For this reason, we must also reimplement the <a href="qpaintdevice.html#metric">metric()</a> function to reflect the metrics of framebuffer.</p>
<a name="implementing-a-custom-window-surface"></a>
<h3>Implementing a Custom Window Surface</h3>
<p>The custom window surface can be derived from the QWSWindowSurface class. QWSWindowSurface manages the memory used when drawing a window.</p>
<pre> class SvgalibSurface : public QWSWindowSurface
 {
 public:
     SvgalibSurface();
     SvgalibSurface(QWidget *w);
     ~SvgalibSurface();

     void setGeometry(const QRect &amp;rect);
     bool isValid() const { return true; }
     bool scroll(const QRegion &amp;region, int dx, int dy);
     QString key() const { return QLatin1String(&quot;svgalib&quot;); }

     bool attach(const QByteArray &amp;) { return true; }
     void detach() {}

     QImage image() const { return QImage(); }
     QPaintDevice *paintDevice() { return pdevice; }
     QPoint painterOffset() const;

 private:
     SvgalibPaintDevice *pdevice;
 };</pre>
<p>We can implement most of the pure virtual functions inherited from QWSWindowSurface as trivial inline functions, except the <a href="qwindowsurface.html#scroll">scroll()</a> function that actually makes use of some hardware acceleration:</p>
<pre> bool SvgalibSurface::scroll(const QRegion &amp;region, int dx, int dy)
 {
     const QVector&lt;QRect&gt; rects = region.rects();
     for (int i = 0; i &lt; rects.size(); ++i) {
         const QRect r = rects.at(i);
         gl_copybox(r.left(), r.top(), r.width(), r.height(),
                    r.left() + dx, r.top() + dy);
     }

     return true;
 }</pre>
<a name="adjusting-the-graphics-driver"></a>
<h3>Adjusting the Graphics Driver</h3>
<p>Finally, we enable the graphics driver to recognize an instance of our custom window surface:</p>
<pre> QWSWindowSurface* SvgalibScreen::createSurface(QWidget *widget) const
 {
     if (base()) {
         static int onScreenPaint = -1;
         if (onScreenPaint == -1)
             onScreenPaint = qgetenv(&quot;QT_ONSCREEN_PAINT&quot;).toInt();

         if (onScreenPaint &gt; 0 || widget-&gt;testAttribute(Qt::WA_PaintOnScreen))
             return new SvgalibSurface(widget);
     }
     return QScreen::createSurface(widget);
 }

 QWSWindowSurface* SvgalibScreen::createSurface(const QString &amp;key) const
 {
     if (key == QLatin1String(&quot;svgalib&quot;))
         return new SvgalibSurface;
     return QScreen::createSurface(key);
 }</pre>
<p>The <a href="qscreen.html#createSurface">createSurface()</a> functions are factory functions that determines what kind of surface a top-level window is using. In our example we only use the custom surface if the given window has the <a href="qt.html#WidgetAttribute-enum">Qt::WA_PaintOnScreen</a> attribute or the <a href="qtopiacore-envvars.html#qt-onscreen-paint">QT_ONSCREEN_PAINT</a> environment variable is set.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
